﻿using WQLWebAPI.Application.Images.Dtos;
using WQLWebAPI.Domain.Abstract;
using WQLWebAPI.Domain.Entities;
using WQLWebAPI.Infrastructure.CommonDtos;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace WQLWebAPI.Application.Images
{
    public class ImagesService : IImageService
    {
        private IBaseRepository<Image> _imagesRepository;
        private IMapper _mapper;

        public ImagesService(IBaseRepository<Image> imagesRepository, IMapper mapper)
        {
            _imagesRepository = imagesRepository;
            _mapper = mapper;


        }

        public async Task<Image> CreateAsync(byte[] value)
        {
            return await _imagesRepository.CreateAsync(new Image { ImageInfo = value });
        }

        public async Task<List<string>> CreateManyAsync(List<byte[]> values)
        {
            var list = new List<string>();
            foreach (var item in values)
            {
                var temp = await _imagesRepository.CreateAsync(new Image { ImageInfo = item });
                list.Add(temp.Id);
            }
            return list;
        }

        public async Task<Image> GetAsync(string id)
        {
            return await _imagesRepository.FirstOrDefaultAsync(id);
        }

        public async Task<List<Image>> GetAsync()
        {
            return await _imagesRepository.GetAsync();
        }

        public async Task<bool> RemovesAsync(List<string> id)
        {
            return await _imagesRepository.RemoveAsync(id);
        }

        public async Task<bool> UpdateAsync(string id, byte[] value)
        {
            var entity = await _imagesRepository.FirstOrDefaultAsync(id);
            if (entity != null)
            {
                entity.ImageInfo = value;
            }
            return await _imagesRepository.UpdateAsync(id, entity);
        }

        public async Task<bool> UpdateManyAsync(List<ImageDto> values)
        {
            foreach (var item in values)
            {
                var entity = await _imagesRepository.FirstOrDefaultAsync(item.Id);
                if (entity != null)
                {
                    entity.ImageInfo = item.ImageInfo;
                }
                await _imagesRepository.UpdateAsync(item.Id, entity);
            }
            return true;

        }
        /// <summary>
        /// 将excel图文内容转为html对象
        /// </summary>
        /// <param name="excelContent">读取的excel内容</param>
        /// <param name="imgUrls">图片服务器地址</param>
        /// <returns></returns>
        public async Task<string> GetHtmlContent(ExcelContentDto excelContent, string imgUrls)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<p>" + excelContent.Content);
            //var urls = HttpContext.Request.Host.Value;
            if (excelContent.Images != null)
            {
                foreach (var item in excelContent.Images)
                {
                    var temp = await CreateAsync(item);
                    //sb.Append($"<img src={imgUrls}/api/Images/{temp.Id}>");
                    sb.Append($"<img src=api/Images/{temp.Id}>");
                }
                sb.Append("</p>");
            }
            else
            {
                sb.Append("</p>");
            }
            return sb.ToString();
        }
    }
}
